<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>调试 VAL_TABLE 解析</title>
    <style>
        body { font-family: monospace; margin: 20px; }
        .log { background: #f5f5f5; padding: 10px; margin: 5px 0; }
        .error { color: red; }
        .success { color: green; }
        .test { border: 1px solid #ccc; margin: 10px 0; padding: 10px; }
    </style>
</head>
<body>
    <h1>调试 VAL_TABLE 解析</h1>
    
    <div class="test">
        <h3>测试1: 最简单的 VAL_TABLE</h3>
        <div id="test1-content">
            <pre>VERSION ""
BU_:
VAL_TABLE_ test 1 "one" 0 "zero" ;
BO_ 100 TestFrame: 8 Vector__XXX</pre>
        </div>
        <button onclick="runTest1()">运行测试1</button>
        <div id="test1-result"></div>
    </div>

    <div class="test">
        <h3>测试2: 复杂的 VAL_TABLE (来自 cadillac 文件)</h3>
        <div id="test2-content">
            <pre>VERSION ""
BU_:
VAL_TABLE_ vt_BooleanValues 1 "true" 0 "false" ;
VAL_TABLE_ DrvrAttnStat 7 "Invalid" 6 "Driver is exhibiting sleep" 5 "Driver is exhibiting microsleep" 4 "Attention is Center Console" 3 "Attention is Drivers Lap" 2 "Attention is Off Road" 1 "Attention is On Road" 0 "Unknown" ;
BO_ 100 TestFrame: 8 Vector__XXX</pre>
        </div>
        <button onclick="runTest2()">运行测试2</button>
        <div id="test2-result"></div>
    </div>

    <script src="dbc-parser.js"></script>
    <script>
        function logTokenizer(content) {
            const tokenizer = new DBCTokenizer(content);
            const tokens = [];
            
            let token = tokenizer.getNextToken();
            while (token.type !== 'Eof') {
                tokens.push({
                    type: token.type,
                    image: token.image,
                    line: tokenizer.line
                });
                token = tokenizer.getNextToken();
            }
            
            return tokens;
        }
        
        function runTest1() {
            const content = document.getElementById('test1-content').textContent.trim();
            const resultDiv = document.getElementById('test1-result');
            
            try {
                // 先看看token化的结果
                console.log('测试1 - Token化结果:');
                const tokens = logTokenizer(content);
                tokens.forEach((token, i) => {
                    console.log(`${i}: ${token.type} = "${token.image}" (行 ${token.line})`);
                });
                
                // 然后尝试解析
                const database = parseDBCContent(content, 'test1.dbc');
                resultDiv.innerHTML = `
                    <div class="success">✓ 解析成功</div>
                    <div>警告数量: ${database.warnings.length}</div>
                    <div>帧数量: ${database.frames.length}</div>
                    ${database.warnings.map(w => `<div class="log">警告: ${w.message} (行 ${w.line})</div>`).join('')}
                `;
            } catch (error) {
                console.error('测试1失败:', error);
                resultDiv.innerHTML = `<div class="error">✗ 解析失败: ${error.message}</div>`;
            }
        }
        
        function runTest2() {
            const content = document.getElementById('test2-content').textContent.trim();
            const resultDiv = document.getElementById('test2-result');
            
            try {
                // 先看看token化的结果
                console.log('测试2 - Token化结果:');
                const tokens = logTokenizer(content);
                tokens.forEach((token, i) => {
                    console.log(`${i}: ${token.type} = "${token.image}" (行 ${token.line})`);
                });
                
                // 然后尝试解析
                const database = parseDBCContent(content, 'test2.dbc');
                resultDiv.innerHTML = `
                    <div class="success">✓ 解析成功</div>
                    <div>警告数量: ${database.warnings.length}</div>
                    <div>帧数量: ${database.frames.length}</div>
                    ${database.warnings.map(w => `<div class="log">警告: ${w.message} (行 ${w.line})</div>`).join('')}
                `;
            } catch (error) {
                console.error('测试2失败:', error);
                resultDiv.innerHTML = `<div class="error">✗ 解析失败: ${error.message}</div>`;
            }
        }
    </script>
</body>
</html> 